﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cherry.Agile.RpcService
{
    /// <summary>
    /// 业务插件解释模块配置装载器
    /// </summary>
    public class ProcessModuleInfo
    {
        public ProcessModuleInfo(object processor)
        {
            ProcessorInstance=processor;
            if(processor is IServiceProcessor)
                this.Process=ProcessInterface;
            else
                this.Process=ProcessFunction;
        }

        private object ProcessorInstance;

        public int BussinessCode { get { return RequestCode >> 16; } }

        public int FunctionCode { get { return RequestCode << 16 >>16; } }
        /// <summary>
        /// 说明
        /// </summary>
        public string Description { get; set; }

        public string ClassName { get; set; }

        public string AssemblyName { get; set; }



        /// <summary>
        /// 接口暂时关闭
        /// </summary>
        public bool Disabled { get; set; }

        /// <summary>
        /// 是否要求验证Token
        /// </summary>
        public bool RequireValidate { get; set; }

        /// <summary>
        /// 是否同时检查手机通知状态
        /// </summary>
        public bool CheckNewNotify { get; set; }

        /// <summary>
        /// 模块是否重用（即单例保持，不需要重复创建）
        /// </summary>
        public bool Reusable { get; set; }

        /// <summary>
        /// 要求具备的权限
        /// </summary>
        public string RequirePermission { get; set; }
        private string _functionName;


        /// <summary>
        /// 请求码
        /// </summary>
        public Int32 RequestCode { get; set; }

        /// <summary>
        /// 处理函数名
        /// </summary>
        public string FunctionName
        {
            get { return _functionName; }
            set
            {
                _functionName = value;
                if (!string.IsNullOrEmpty(value))
                {
                    this.Process = ProcessFunction;
                }
            }
        }
        /// <summary>
        /// 处理接口调用代理
        /// </summary>
        public Action<ProcessContext> Process { get; set; }

        protected void ProcessInterface(ProcessContext context)
        {
            ((IServiceProcessor)ProcessorInstance).Process(context);
        }

        protected void ProcessFunction(ProcessContext context)
        {
            ProcessorInstance.GetType().InvokeMember(FunctionName, System.Reflection.BindingFlags.InvokeMethod, null, ProcessorInstance, new object[] { context });
        }

    }
}
